﻿using Koala.Pro.Common;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace Koala.Pro.Helpers
{
    public class JsonLanguage
    {
        private static object syncRoot = new object();

        private static JsonLanguage instance = null;

        private Dictionary<string, string> resources;

        private Dictionary<string, string> LangKeyDict = new Dictionary<string, string>();

        public static JsonLanguage Default
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new JsonLanguage();
                        }
                    }
                }
                return instance;
            }
        }

        public bool HasResource
        {
            get
            {
                if (resources != null && resources.Count > 0)
                {
                    return true;
                }
                return false;
            }
        }

        private JsonLanguage()
        {
            LoadLanguage();
        }

        public void LoadLanguage(string language = "")
        {
            if (string.IsNullOrEmpty(language))
            {
                language = Thread.CurrentThread.CurrentUICulture.Name;
            }
            resources = new Dictionary<string, string>();
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"lang/{language}");
            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    LoadFile(file);
                }
            }
        }

        private void LoadFile(string file)
        {
            string value = File.ReadAllText(file, Encoding.UTF8);
            if (string.IsNullOrEmpty(value))
            {
                return;
            }
            Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(value);
            foreach (string key in dictionary.Keys)
            {
                if (!resources.ContainsKey(key))
                {
                    resources.Add(key, dictionary[key]);
                }
                else
                {
                    resources[key] = dictionary[key];
                }
            }
        }

        public void Translate(bool sorted = true, string language = "")
        {
            string from = "zh";
            string to = "en";
            if (string.IsNullOrEmpty(language))
            {
                language = Thread.CurrentThread.CurrentUICulture.Name;
            }
            if (!string.IsNullOrEmpty(language))
            {
                to = language.Substring(0, language.IndexOf('-'));
            }
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"lang\\{language}");
            if (Directory.Exists(path))
            {
                string[] files = Directory.GetFiles(path, "*.json", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    TranslateFile(file, from, to, sorted);
                }
            }
            LoadLanguage();
        }

        private void TranslateFile(string file, string from = "zh", string to = "en", bool sorted = true)
        {
            string value = File.ReadAllText(file, Encoding.UTF8);
            if (string.IsNullOrEmpty(value))
            {
                return;
            }
            Dictionary<string, string> dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(value);
            string[] array = dictionary.Keys.ToArray();
            for (int i = 0; i < dictionary.Keys.Count; i++)
            {
                string text = array[i];
                if (string.IsNullOrWhiteSpace(dictionary[text]))
                {
                    string value2 = TranslationHelper.Translate(text, from, to);
                    if (!string.IsNullOrWhiteSpace(value2))
                    {
                        dictionary[text] = value2;
                    }
                }
            }
            string text2 = "";
            text2 = ((!sorted) ? JsonConvert.SerializeObject(dictionary, Formatting.Indented) : JsonConvert.SerializeObject(new SortedDictionary<string, string>(dictionary), Formatting.Indented));
            File.WriteAllText(file, text2, Encoding.UTF8);
        }

        public string GetString(string key)
        {
            if (!LangKeyDict.ContainsKey(key))
            {
                LangKeyDict.Add(key, key);
            }
            string text = LangKeyDict[key];
            string text2 = text;
            if (resources != null && resources.ContainsKey(text))
            {
                text2 = resources[text];
                if (string.IsNullOrWhiteSpace(text2))
                {
                    text2 = text;
                }
            }
            string text3 = StringUtil.ToProperCase(text2);
            if (!LangKeyDict.ContainsKey(text3))
            {
                LangKeyDict.Add(text3, key);
            }
            return text3;
        }
    }
}
